using System.Linq;
using SudokuHelper.Model.Values;

namespace SudokuHelper.Model {
    public class DeskValidator : ValueRelationDispatcher {
        #region Private members

        private readonly ISudokuDesk context;
        private readonly int row;
        private readonly int col;

        private bool result = true;
        #endregion

        #region Properties

        public bool Result {
            get { return result; }
        }

        #endregion

        #region Initialization

        public DeskValidator(ISudokuDesk context, int row, int col) {
            this.context = context;
            this.row = row;
            this.col = col;
        }

        #endregion

        #region Utility methods

        public bool Validate(IValue value) {
            result = true;
            context.ForeachInArea(row, col, (r, c) => Dispatch(value, context[r, c]));
            return result;
        }

        #endregion

        #region Overrides of ValueRelationDispatcher

        public override void Perform(UserValue userValue1, UserValue userValue2) {
            result &= (userValue1.Value != userValue2.Value);
        }

        public override void Perform(UserValue userValue, CalculatedValue calculatedValue) {
            result &= (userValue.Value != calculatedValue.Value);
        }

        public override void Perform(UserValue userValue, ValueRange valueRange) {
        }

        public override void Perform(CalculatedValue calculatedValue1, CalculatedValue calculatedValue2) {
            result &= (calculatedValue1.Value != calculatedValue2.Value);
        }

        public override void Perform(CalculatedValue calculatedValue, ValueRange valueRange) {
        }

        public override void Perform(ValueRange range1, ValueRange range2) {
        }

        #endregion
    }
}
